<template>
  <div>
    <el-dialog
      :title="info.type == 'add' ? '新增菜单' : '编辑菜单'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <span>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="角色名称" prop="roleid">
            <el-select v-model="ruleForm.roleid" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.rolename"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="管理员名称" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="管理员密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="menus">
            <el-radio-group v-model="ruleForm.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="2">禁用</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >立即创建</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="info.isShow = false">取 消</el-button>
        <el-button type="primary" @click="info.isShow = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { userAdd, userEdit,roleList } from "@/api.js";
export default {
  props: ["info"],
  data() {
    return {
      ruleForm: {
        roleid:'',
        username: "",
        password:"",
        status: 1,
      },
      options:[],
      rules: {
        username: [
          { required: true, message: "请输入菜单标题", trigger: "blur" },
        ],
        roleid: [
            { required: true, message: '请选择角色名称', trigger: 'change' }
          ],
      },
      defaultProps: {
        children: "children",
        label: "title",
      },
    };
  },
  async created() {
    let res=await roleList()
    this.options=res.list
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.info.type === "add") {
            let res = await userAdd(this.ruleForm);
          } else {
            let res = await userEdit(this.ruleForm);
          }
          this.info.isShow = false;
          this.$emit("update");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //给表单复制
    setValue(info) {
      this.ruleForm = { ...info };
    },
  },
};
</script>
<style scoped>
</style>